﻿'
' Copyright (c) 2010-2012, MatthiWare
'
' Permission is hereby granted, free of charge, to any person obtaining a
' copy of this software and associated documentation files (the "Software"),
' to deal in the Software without restriction, including without limitation
' the rights to use, copy, modify, merge, publish, distribute, sublicense,
' and/or sell copies of the Software, and to permit persons to whom the
' Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in
' all copies or substantial portions of the Software. 
' You shall include 'MatthiWare' in the credits/about section of the program
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
' IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
' FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
' AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
' LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
' FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
' DEALINGS IN THE SOFTWARE.
'

Imports SharpDX
Imports SharpDX.Direct2D1

Public Class InventorySlot

    Public Info As InvSlotInfo = Nothing

    Public Shared INV_WIDHT_S As Int32 = 46
    Public Shared INV_HEIGHT_S As Int32 = 46

    Public X As Int32
    Public Y As Int32

    Public Sub New(x As Int32, y As Int32, e As Core.Base.GameCoreEventArgs)
        Me.X = x
        Me.Y = y
        Info = New InvSlotInfo(e)
    End Sub

    Public Sub Render(e As Core.Base.DrawEventArgs, s As Boolean)

        e.Target.DrawBitmap(Tile.InvBack, New DrawingRectangleF(0, 0, INV_WIDHT_S, INV_HEIGHT_S), 1.0F, BitmapInterpolationMode.Linear)
        e.Target.DrawBitmap(Item.ItemImageCollection(Me.Info.Item.Type), New DrawingRectangleF(CInt(INV_WIDHT_S / 3), CInt(INV_HEIGHT_S / 3), Tile.TileSize, Tile.TileSize), 1.0F, BitmapInterpolationMode.Linear)
        Dim tf As New SharpDX.DirectWrite.TextFormat(e.Core.FactoryWrite, "Comic Sans MS", 12)
        Dim tx As New SharpDX.DirectWrite.TextLayout(e.Core.FactoryWrite, Me.Info.Stack, tf, Single.MaxValue, Single.MaxValue)
        Dim brsh As New SharpDX.Direct2D1.SolidColorBrush(e.Target, Color.White)
        e.Target.DrawText(Me.Info.Stack, tf, New DrawingRectangleF((INV_WIDHT_S / 3) + Tile.TileSize - 1, (INV_HEIGHT_S / 3) + Tile.TileSize - 3, tx.Metrics.Width, tx.Metrics.Height), brsh)

        'e.Target.DrawText(Me.Info.Stack, New Font("Comic Sans MS", 12, FontStyle.Bold, GraphicsUnit.Pixel), Brushes.White, (INV_WIDHT_S / 3) + Tile.TileSize - 1, (INV_HEIGHT_S / 3) + Tile.TileSize - 3)

        If (s) Then
            e.Target.DrawRectangle(New DrawingRectangleF(X * INV_WIDHT_S, (Y * INV_HEIGHT_S) + 25, INV_WIDHT_S - 1, INV_HEIGHT_S - 1), New SolidColorBrush(e.Target, Color.Red), 3)

            Dim strText As String = IIf(Info.Item.Name = "NULL", "", Info.Item.Name)
            tf = New SharpDX.DirectWrite.TextFormat(e.Core.FactoryWrite, "Comic Sans MS", 16)
            tx = New SharpDX.DirectWrite.TextLayout(e.Core.FactoryWrite, strText, tf, Single.MaxValue, Single.MaxValue)
            brsh = New SharpDX.Direct2D1.SolidColorBrush(e.Target, Color.White)
            e.Target.DrawText(strText, tf, New DrawingRectangleF(0, 0, tx.Metrics.Width, tx.Metrics.Height), brsh)
            'e.Target.DrawText(IIf(Info.Item.Name = "NULL", "", Info.Item.Name), New Font("Comic Sans MS", 16, FontStyle.Bold, GraphicsUnit.Pixel), Brushes.White, 1, 1)
        End If
    End Sub

    Public Sub Empty()
        Info.Stack = 0
        Info.Item = Nothing
    End Sub

    Public Sub Add(amount As Int32)
        If (Info.Item.IsStackable) Then
            Dim nextStack As Int32 = Info.Stack + amount
            If (nextStack < 100) Then
                Info.Stack = nextStack
            End If
        End If
    End Sub

    Public Sub Remove(amount As Int32)
        If (amount <= Info.Stack And amount > 0) Then
            Info.Stack -= amount
        End If
    End Sub

End Class
